<?php
error_reporting(E_ERROR | E_PARSE);
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = $_SESSION['user'];
$order = mysql_real_escape_string($_POST['id']);

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);
$username = $row['username'];
$access_level = $row['type'];

$returnArray = array();

$result = mysql_query("SELECT * FROM match_history WHERE order_id = $order ORDER BY timestamp DESC") or die(mysql_error());

$y = 0;

$all_wins = 0;
$all_losses = 0;

while($row = mysql_fetch_array($result))
{
	array_push($returnArray, $row);
	if($row['win'] == 0)
		$all_losses++;
	else
		$all_wins++;
	$y++;
}
$pages = ceil($y/5);

$tier = "";
$rank = "";
$last_match_result = mysql_query("SELECT * FROM match_history WHERE order_id = $order ORDER BY timestamp DESC LIMIT 1") or die(mysql_error());
//$tier = $row['start_tier'];
//$rank = $row['start_division'];
if(mysql_num_rows($last_match_result) == 0)
{
	$lp = $order_row['beginning_lp'];
	$tier = $order_row['start_tier'];
	$rank = numberToRoman($order_row['start_division']);
}
else
{
	$last_match = mysql_fetch_array($last_match_result);
	$lp = $last_match['lp_at_end'];
	if($last_match['tier_at_end'] != null && $last_match['tier_at_end'] != "")
	{
		$tier = $last_match['tier_at_end'];
	}
	if($last_match['rank_at_end'] != null && $last_match['rank_at_end'] != "")
	{
		$rank = $last_match['rank_at_end'];
	}

	//$tier = $last_match['tier_at_end'];
	//$rank = $last_match['rank_at_end'];
}
if($tier == "")
{
	$lp = $order_row['beginning_lp'];
	$tier = $order_row['start_tier'];
	$rank = numberToRoman($order_row['start_division']);
}

$x = 0;
$champ_array = array();

while(isset($returnArray[$x]))
{
	$champ_array[$x] = $returnArray[$x]['champion'];
	$x++;
}

$champs = array_count_values($champ_array);

arsort($champs);
$champ_keys = array_keys($champs);

$x = 0;

$win_loss = array();

while(isset($champ_keys[$x]))
{
	$y = 0;
	$win_loss[$x] = array();
	$win_loss[$x]['win'] = 0;
	$win_loss[$x]['loss'] = 0;
	while(isset($returnArray[$y]))
	{
		if($returnArray[$y]['champion'] == $champ_keys[$x])
		{
			if($returnArray[$y]['win'] == 1)
				$win_loss[$x]['win']++;
			else
				$win_loss[$x]['loss']++;
		}
		$y++;
	}
	$x++;
}
/*$champ_lookup = array();

$response = Unirest::get(
  "https://teemojson.p.mashape.com/datadragon/champion",
  array(
    "X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
  )
);

$decode = json_decode($response->raw_body)->data;
$x = 0;

foreach ($decode as $item)
{
	$champ_lookup[$x] = array();
	$champ_lookup[$x]['id'] = $item->key;
	$champ_lookup[$x]['name'] = $item->id;
	$x++;
}*/
//var_dump($champ_lookup); die;

//var_dump($champ_lookup[key_to_name($champ_lookup, (string)$returnArray[0]['champion'])]['name']); die;

function key_to_name($products, $needle)
{
   foreach($products as $key => $product)
   {
      if ( $product['id'] === $needle )
         return $key;
   }
   return false;
}
//var_dump($decode->data->summonerLeagues->array[0]); die;
function numberToRoman($num) 
 {
     // Make sure that we only use the integer portion of the value
     $n = intval($num);
     $result = '';
 
     // Declare a lookup array that we will use to traverse the number:
     $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
     'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
     'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
 
     foreach ($lookup as $roman => $value) 
     {
         // Determine the number of matches
         $matches = intval($n / $value);
 
         // Store that many characters
         $result .= str_repeat($roman, $matches);
 
         // Substract that from the number
         $n = $n % $value;
     }
 
     // The Roman numeral should be built, return it
     return $result;
 }

 function romanToNumber($roman)
 {
	 $romans = array(
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1,
);

$result = 0;

foreach ($romans as $key => $value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
return $result;
}

 function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}
$x = 0;

while(isset($returnArray[$x]) && $x < 5)
{
	if($returnArray[$x]['win'] == 0)
		echo '<div class="match_loss">';
	else
		echo '<div class="match_win">';

	if($returnArray[$x]['win'] == 0)
		echo '<div class="result">Loss</div>';
	else
		echo '<div class="result">Win</div>';

	echo '<div class = "champion"><div style="background-image: url(images/champions/'.$returnArray[$x]['champion'].'.png); background-size: contain; height: 72px; width: 72px;"></div></div>';

	echo '<div class="match_details"><small><i>'.$tier.' '.$rank.', LP: '.$returnArray[$x]['lp_at_end'].'</i></small><br><small>'.$returnArray[$x]['date'].'</small><br><small>'.floor((time() - $returnArray[$x]['timestamp'])/3600).' Hours Ago</small></div>';

	echo '<div class="spells"><img src="images/summoner/'.$returnArray[$x]['spell1'].'.png"><img src="images/summoner/'.$returnArray[$x]['spell2'].'.png"></div>';

	echo '<div class="items">';

	if(isset($returnArray[$x]['item0']) && $returnArray[$x]['item0'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item0'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item1']) && $returnArray[$x]['item1'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item1'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item2']) && $returnArray[$x]['item2'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item2'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item3']) && $returnArray[$x]['item3'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item3'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item4']) && $returnArray[$x]['item4'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item4'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item5']) && $returnArray[$x]['item5'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item5'].'_32.png" alt="">';
	}
	   echo '<br></div>';
	
	echo '<div class="match_stats">'.$returnArray[$x]['kills'].' Kills<br>'.$returnArray[$x]['deaths'].' Deaths<br>'.$returnArray[$x]['assists'].' Assists</div>';

	echo '<div class="minions">'.$returnArray[$x]['minions'].'</div>';

	echo '<div class="gold">'.$returnArray[$x]['gold'].'g</div>';

	echo '<div class="booster">'.$returnArray[$x]['booster'].'</div>';

	echo '</div>';

	$x++;
}
	?>